%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                     Lab 3: Digital Filter Design                        %
%                   EE558L: Section 1 (F: 1200-1440)                      %
%                            Dr. Nagaraj                                  %
%                     Author: Michael Spinali                             %
%                             813488956                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Create Fresh Environment
close all
clear all
clc

% Define Constants
mp = 2;                             % Message Peak
mue = 1;                            % Modulation Index
A = mp/mue;                         % DC Shift

n = 0:1:299;                        % Time vector (samples)
f  = 100;                           % Message Frequency
fs = 30E3;                          % Sampling Frequency
fc = 1.5E3;                         % Carrier Frequency

mn = mp*sin(2*pi*f*n/fs);           % Message
xn = (A + mn).*cos(2*pi*fc*n/fs);   % Modulated signal
subplot(3,1,1);
plot(n,xn);
grid on;
axis([0 length(n) -4.2 4.2]);
title('AM Modulated 100Hz Sine Wave','fontsize',14);
xlabel('Time (samples)','fontsize',14);
ylabel('Amplitude','fontsize',14);

% Rectified Signal
subplot(3,1,2);
yn1 = abs(xn);
plot(n,yn1);
grid on;
axis([0 length(n) -0.2 4.2]);
title('Rectified AM Modulated 100Hz Sine Wave','fontsize',14);
xlabel('Time (samples)','fontsize',14);
ylabel('Amplitude','fontsize',14);

%Filtered LPF signal
N=31;                                       % Filter Order
Fc = 400/(fs/2);                            % Cutoff Frequency (0-400Hz BW)
Hf = design(fdesign.lowpass('N,Fc',N,Fc));  % Filter Design
yn2 = filter(Hf,yn1);                       % Apply Filter

subplot(3,1,3);
plot(n,yn2);
grid on;
axis([0 length(n) -0.3 3]);
title('DeModulated 100Hz Sine Wave','fontsize',14);
xlabel('Time (samples)','fontsize',14);
ylabel('Amplitude','fontsize',14);
